package com.shark;

import cn.hutool.core.util.IdUtil;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.UUID;

/**
 * @auther shark
 * @create 2024-08-12 15:17
 */
@SpringBootTest
public class RabbitMQTestBase
{
    @Resource
    private RabbitTemplate rabbitTemplate;

    // 在简单模式下，没有用到指定的交换机
    public static final String EXCHANGE_DIRECT = "";
    // 在简单模式下，消息直接发送到队列，此时生产者端需要把队列名称从路由键参数这里传入
    public static final String ROUTING_KEY_SIMPLE = "atguigu.queue.simple";
    @Test
    public void testSendMessageSimple() {
        // 发送消息
        rabbitTemplate.convertAndSend(
                EXCHANGE_DIRECT,   	// 指定交换机名称
                ROUTING_KEY_SIMPLE, // 指定路由键名称
                "Hello atguigu:  "+ IdUtil.simpleUUID());   // 消息内容，也就是消息数据本身

    }




    //1.2 工作队列模式代码
    public static final String ROUTING_KEY_WORK = "atguigu.queue.work";
    @Test
    public void testSendMessageWork() {
        for (int i = 1; i <= 10; i++) {
            rabbitTemplate.convertAndSend(
                    "",
                    ROUTING_KEY_WORK,
                    "Hello atguigu " + i);
        }
    }




    //3 Publish/Subscribe发布订阅模式
    public static final String EXCHANGE_FANOUT = "atguigu.exchange.fanout";
    @Test
    public void testSendMessageFanout() {
        rabbitTemplate.convertAndSend(EXCHANGE_FANOUT, "", "Hello fanout ~");
    }



    //4 Routing路由模式
    public static final String ROUTING_KEY_GOOD = "atguigu.routing.key.good";
    @Test
    public void testSendMessageRouting() {
        rabbitTemplate
                .convertAndSend(
                        "atguigu.exchange.direct",
                        ROUTING_KEY_GOOD,
                        "Hello routing with key ~ 0620");
    }






    /**
         通配符规则：
         #：匹配零个或多个词       举例：   item.#：能够匹配item.insert.abc 或者 item.insert
         *：匹配不多不少恰好1个词  举例： item.*：只能匹配item.insert
     */
    public static final String EXCHANGE_TOPIC = "atguigu.exchange.topic";
    public static final String ROUTING_KEY_ERROR = "#.error";
    public static final String ROUTING_KEY_ORDER = "order.*";
    public static final String ROUTING_KEY_ALL = "*.*";
    @Test
    public void testSendMessageTopic() {
        //rabbitTemplate.convertAndSend(EXCHANGE_TOPIC, "order.info", "message order info ...");
        //rabbitTemplate.convertAndSend(EXCHANGE_TOPIC, "goods.info", "message goods info ...");
        rabbitTemplate.convertAndSend(EXCHANGE_TOPIC, "a.b.goods.error", "message goods error ...");
    }
}
